import {createRouter, createWebHashHistory} from 'vue-router'
import Layout from '@/layout/index.vue'
import Common from '@/layout/common.vue'

import {state, action} from '@/store/index'
import token from '@/store/token'
const routes = [
    {
        name: 'common',
        path: '/common/:pathMatch(.*)',
        component: Common,
    },
    {
        name: 'admin',
        path: '/:pathMatch(.*)',
        component: Layout,
    },
]
const router = createRouter({
    history: createWebHashHistory(),
    routes
})

router.beforeEach(async (to, from, next) => {
    //跳转登录页
    // if (!token.get() && to.path !== state.loginRoute && to.path.indexOf(state.commonRoutePrefix) !== 0) {
    //     return next(state.loginRoute + '?redirect=' + to.fullPath)
    // }
    if(to.path.indexOf(state.commonRoutePrefix) !== 0 && to.path !== state.loginRoute && !state.systemInfo){
        await action.systemInfo()
    }
    if(to.fullPath !== '/' && to.fullPath !== '/common/curd'){
        await action.loadComponent(to.fullPath)
    }
    action.watchRouteMenu(to.fullPath)
    return next()
})
router.afterEach((to) => {

})
export default router

